//
//  UIView+Handle.m
//  JuanRoobo
//
//  Created by Zhi Kuiyu on 15/2/27.
//  Copyright (c) 2015年 Zhi Kuiyu. All rights reserved.
//

#import "UIView+Handle.h"
#import <objc/runtime.h>

#define WindowSize [UIScreen mainScreen ].bounds.size

@implementation UIView (Handle)
@dynamic bgView;


- (void)circlebackGround{
    
    UIView * view = [[UIView alloc] initWithFrame:self.bounds];
    view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
    UIViewAutoresizingFlexibleWidth |
    UIViewAutoresizingFlexibleHeight ;
    [self insertSubview:view atIndex:0];
    view.userInteractionEnabled = NO;
    view.clipsToBounds = YES;
    view.layer.frame = self.bounds;
    view.layer.borderColor = [UIColor colorWithRed:0.525 green:0.584 blue:0.643 alpha:1.000].CGColor;
    view.layer.cornerRadius = 22;
    view.layer.borderWidth = 1;
    [view.layer setShadowOffset:CGSizeMake(0, 0)];
    [view.layer setShadowColor:[[UIColor clearColor] CGColor]];
    [view.layer setShadowOpacity:1];
    view.layer.shadowRadius = 1.0;//半径
    
    [self setBgView:view];
    
    
}

- (UIViewController*)viewController {

    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            if([nextResponder isKindOfClass:[UINavigationController class]]){
                return ((UINavigationController*)nextResponder).visibleViewController;
            }
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}


- (UIFont *)GetFontSize:(float)fontsizeOffset{

//    NSInteger fontsize = [[NSUserDefaults standardUserDefaults] integerForKey:@"DefaultFontSize"];
//    if(fontsize < 14){
//        fontsize = 16;
//        [[NSUserDefaults standardUserDefaults] setInteger:14 forKey:@"DefaultFontSize"];
//        [[NSUserDefaults standardUserDefaults] synchronize] ;
//    }
    
//TODO:GetFontSize
    return [UIFont fontWithName:@"" size:16 + fontsizeOffset];
    
}

+ (float) getRealHelght:(float)currentHight{
    
    
    NSAssert(!CGSizeEqualToSize(WindowSize, CGSizeZero), @"获取屏幕大小失败") ;
    float height = WindowSize.height;
    if(WindowSize.height < 500){
        height -= 30;
    }
    return currentHight * (height/568);
    
    
    return [ UIScreen mainScreen ].bounds.size.height;
    
}
+ (float) getRealWidth:(float)currentWidth{
    NSAssert(!CGSizeEqualToSize(WindowSize, CGSizeZero), @"获取屏幕大小失败") ;

    return currentWidth * (WindowSize.width/320);
    
    
}

- (void)showWarmAnimal{
//    [self.bgView.layer setShadowColor:[[UIColor orangeColor] CGColor]];
    self.bgView.layer.borderColor = [UIColor colorWithRed:0.525 green:0.584 blue:0.643 alpha:1.000].CGColor;

    [UIView  beginAnimations:@"showWarmAnimal" context:nil];
    [UIView setAnimationDuration:.15];
    [UIView setAnimationRepeatCount:4];
    self.bgView.alpha = .4;
    self.alpha = .7;
    //    self.bgView.backgroundColor = [UIColor colorWithWhite:1 alpha:.1];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(anmailStop:)];
    [UIView commitAnimations] ;
}

- (void)anmailStop:(id)sender{
    self.bgView.alpha = 1;
    self.alpha = 1;
    [self.bgView.layer setShadowColor:[[UIColor clearColor] CGColor]];
    self.bgView.backgroundColor = [UIColor clearColor];
    
}




- (void)setBgView:(UIView *)bgView{
    
    objc_setAssociatedObject(self, @"bgViewKey", bgView, OBJC_ASSOCIATION_RETAIN);
}

- (UIView *)bgView{
    return objc_getAssociatedObject(self, @"bgViewKey");
}

@end
